package tests;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import junit.framework.TestCase;
import server.profiledGame;

import common.Player;
import common.Profile;


public class profiledGameTest extends TestCase{
	  List<Profile> profiles = new ArrayList<Profile>();
	  Map<Integer,List<Player>> finalstandings;
	  profiledGame PG;
	  ReverseIntegerComparator comparator = new ReverseIntegerComparator();
	  
	  public class ReverseIntegerComparator implements Comparator<Integer> {
			public int compare(Integer num1, Integer num2) {
	            return num2 - num1;
	        }
	    }
	
	protected void setUp() {
	
		
	  List<Player> players1 = new ArrayList<Player>();
	  List<Player> players2 = new ArrayList<Player>();
	  List<Player> players3 = new ArrayList<Player>();
	  List<Player> players4 = new ArrayList<Player>();
	  List<Player> players5 = new ArrayList<Player>();
	  List<Player> players6 = new ArrayList<Player>();
	 
	  
	  
	  Profile p1 = new Profile("nir","nir1","nir@gmail",1000);
	  profiles.add(p1);
	  Profile p2 = new Profile("gilad","gilad1","gilad@gmail",2000);
	  profiles.add(p2);
	  Profile p3 = new Profile("dan","dan1","dan@gmail",3000);
	  profiles.add(p3);
	  Profile p4 = new Profile("amit","amit1","amit@gmail",4000);
	  profiles.add(p4);
	  Profile p5 = new Profile("oded","oded1","oded@gmail",5000);
	  profiles.add(p5);
	  Profile p6 = new Profile("alon","alon1","alon@gmail",6000);
	  profiles.add(p6);
	 
	  
	  PG = new profiledGame(profiles, null, null);
	  
	  Player pl1 = new Player(p1.getUserName(),null);
	  players1.add(pl1);
	  Player pl2 = new Player(p2.getUserName(),null);
	  players2.add(pl2);
	  Player pl3 = new Player(p3.getUserName(),null);
	  players3.add(pl3);
	  Player pl4 = new Player(p4.getUserName(),null);
	  players4.add(pl4);
	  Player pl5 = new Player(p5.getUserName(),null);
	  players5.add(pl5);
	  Player pl6 = new Player(p6.getUserName(),null);
	  players6.add(pl6);
	  
	  finalstandings = new TreeMap<Integer,List<Player>>(comparator);
	  
	  finalstandings.put(6000, players1);
	  finalstandings.put(5000, players2);
	  finalstandings.put(4000, players3);
	  finalstandings.put(3000, players4);
	  finalstandings.put(2000, players5);
	  finalstandings.put(1000, players6);
	}
	
	public void testupdateRanking(){
		int i = 0, j = 0;
		String name;
		
		List<Profile> toRemove = new ArrayList<Profile>();
		Integer[] listOfMoney = new Integer[6];
		finalstandings.keySet().toArray(listOfMoney);
		
		for(i = 0; i<finalstandings.size(); i++){
			
			for(j = 0; j < finalstandings.get(listOfMoney[i]).size(); j++){
				
				name = finalstandings.get(listOfMoney[i]).get(j).getName();

				for(Profile p : profiles){
					if (p.getUserName().equals(name) && p.isGuest()){
						toRemove.add(p);
						finalstandings.get(listOfMoney[i]).remove(j);
					}
				}
			}
		}
		
		for(Profile p : toRemove){
			profiles.remove(p);
		}
		
		PG.updateRanking(profiles, finalstandings);
 	    
		assertEquals(1210,profiles.get(0).getRating());
		assertEquals(2160,profiles.get(1).getRating());
		assertEquals(3110,profiles.get(2).getRating());
		assertEquals(4040,profiles.get(3).getRating());
		assertEquals(4990,profiles.get(4).getRating());
		assertEquals(5915,profiles.get(5).getRating());
	

	}
}
